package meterMonitor.controller;

import meterMonitor.biz.ExportMonitorDetailBiz;
import meterMonitor.framework.auth.AuthorityType;
import meterMonitor.framework.auth.FireAuthority;
import meterMonitor.model.MonitorDetail;
import meterMonitor.util.excel.ExcelUtil;
import meterMonitor.util.excel.domain.ExcelColumn;
import meterMonitor.util.excel.domain.ExcelHead;
import meterMonitor.vo.ReqDetails;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2019/2/22 0022.
 */
@RequestMapping("originalReport")
@RestController
public class ExportDetailsController extends BaseController {

    @Autowired
    ExportMonitorDetailBiz exportMonitorDetailBiz;

    @RequestMapping("originalReport.do")
    @FireAuthority(authorityTypes = AuthorityType.SystemAuth)
    public ModelAndView index(ModelAndView mv) {
        return mv;
    }

    @RequestMapping("findAll.do")
    @ResponseBody
    @FireAuthority(authorityTypes = AuthorityType.SystemAuth)
    public List<MonitorDetail> getAllDetals(ReqDetails reqDetails) {
        try {
            return exportMonitorDetailBiz.selectAll(reqDetails);
        } catch (Exception e) {
            this.logger.error(e.getMessage(), e);
        }
        return null;
    }

    @RequestMapping("export.do")
    @ResponseBody
    @FireAuthority(authorityTypes = AuthorityType.SystemAuth)
    public void export(HttpServletRequest request, HttpServletResponse response) {
        try {
            String startDate = request.getParameter("startDate");
            String endDate = request.getParameter("endDate");
            String meterCode = request.getParameter("meterCode");
            ReqDetails reqDetails = new ReqDetails();
            reqDetails.setStartDate(startDate);
            reqDetails.setEndDate(endDate);
            reqDetails.setMeterCode(meterCode);
            List<MonitorDetail> list = exportMonitorDetailBiz.selectAll(reqDetails);
            //导出
            ExcelHead head = new ExcelHead();
            head.setColumns(setColumns());
            //生成Excel文件流
            ExcelUtil.getInstanse().exportExcel(head, "导出模板", list, response);
        } catch (Exception e) {
            this.logger.error(e.getMessage(), e);
        }
    }

    //构建Excel列表
    private List<ExcelColumn> setColumns() {

        List<ExcelColumn> columns = new ArrayList<>();
        String[] fields = {"pkid", "meterCode", "electricQty", "currentQuantiry", "intervalQuantity", "intervalSec", "uploadDate", "uploadTime", "ownerTimer", "monitorId", "addedTime", "lastModifyTime", "monitorHourId", "receiveQuantiry", "receiveIntervalQuantity"};
        String[] headers = {"序号", "芯片编号", "电量", "当前读表数", "间隔使用量", "间隔时间（秒）", "所属日期", "上传时间", "所属时间段", "对应monitor编号", "数据上传时间", "最后修改时间", "对应monitor_hour编号", "原始读表数", "原始间隔使用量"};
        for (int i = 0; i < fields.length; i++) {
            columns.add(new ExcelColumn(i, fields[i], headers[i]));
        }
        return columns;

    }

}
